/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package API;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class TextBox extends Item {

    private String textPass;
    private int pointer;
    private boolean isShowPointer = false;
    private int textType;
    private static char keyCodesLowCaseVi[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'a', 'ă', 'â', 'b', 'c', '2'}, {'d', 'đ', 'e', 'ê', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', 'ô', 'ơ', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'ư', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}};
    private static char keyCodesLowCase[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'a', 'b', 'c', '2'}, {'d', 'e', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}};
    private static char keyCodesUpCase[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'A', 'B', 'C', '2'}, {'D', 'E', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'V', '8'}, {'X', 'Y', 'Z', 'W', '9'}};
    private static char keyCodesUpCaseVi[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '\'', '\"', '-', '(', ')', '/'}, {'A', 'Ă', 'Â', 'B', 'C', '2'}, {'D', 'Đ', 'E', 'Ê', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', 'Ô', 'Ơ', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'Ư', 'V', '8'}, {'X', 'Y', 'Z', 'W', '9'}};
    private static char keyCodesNumber[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private int mode = 0;
    private Timer timerRunner;
    private TimerTask timerTask;
    private Canvas parent;
    private boolean isRunning;
    private String[] strMode = {"abc", "ABC", "123"};

    public TextBox(String text, int textType, int x, int y, int width) {
        if (text == null) {
            this.text = "";
        } else {
            this.text = text;
        }
        this.pointer = text.length();
        this.textType = textType;
        if (this.textType == TextType.PASSWORD) {
            textPass = "";
            for (int i = 0; i < this.text.length(); i++) {
                textPass += "*";
            }
        }
        this.x = x;
        this.y = y;
        this.width = width;
        timerRunner = new Timer();
        timerTask = new TimerTask() {

            public void run() {
                while (isRunning) {
                    try {
                        Thread.sleep(500);
                        ResetPassword();
                        isShowPointer = !isShowPointer;
                        parent.repaint();
                    } catch (Exception e) {
                    }
                }
            }
        };
        isRunning = true;
        timerRunner.schedule(timerTask, 0);
    }

    public void SetSelect() {
        isSelect = true;
        timerRunner.schedule(timerTask, 0);
    }

    public void StopSelect() {
        isSelect = false;
        isRunning = false;
    }

    public void SetParent(Canvas parent) {
        this.parent = parent;
    }

    public int getHeight() {
        return font.getHeight() + 5;
    }

    public boolean isIsShowPointer() {
        return isShowPointer;
    }

    public void setIsShowPointer(boolean isShowPointer) {
        this.isShowPointer = isShowPointer;
    }

    public void Paint(Graphics g) {
        g.setColor(60);
        g.drawRoundRect(x, y, width, font.getHeight() + 5, cornerRadius, cornerRadius);
        g.setColor(200, 200, 200);
        g.drawRoundRect(x + 1, y + 1, width - 1, font.getHeight() + 4, cornerRadius, cornerRadius);
        g.setColor(0xffffff);
        g.fillRoundRect(x + 1, y + 1, width - 2, font.getHeight() + 3, cornerRadius, cornerRadius);
        g.setColor(0);
        g.setFont(font);
        String textDisplay = text;
        int widthStrMode = font.stringWidth(strMode[mode]) + 2;
        if (this.textType == TextType.PASSWORD) {
            textDisplay = textPass;
        }
        if (isSelect && isShowPointer) {
            if (font.substringWidth(textDisplay, 0, pointer) > width - 1 - widthStrMode) {
                g.drawLine(x + width - 2 - widthStrMode, y + 2, x + width - 2 - widthStrMode, y + font.getHeight() + 2);
            } else {
                g.drawLine(x + 2 + font.substringWidth(textDisplay, 0, pointer), y + 2, x + 2 + font.substringWidth(textDisplay, 0, pointer), y + font.getHeight() + 2);
            }
        }
        if (font.stringWidth(textDisplay) > width - 2 - widthStrMode) {
            String str = "";
            int i = 0;
            if (font.substringWidth(textDisplay, 0, pointer) > width - 2 - widthStrMode) {
                while (true) {
                    str = textDisplay.substring(i, pointer);
                    if (font.stringWidth(str) <= width - 2 - widthStrMode) {
                        break;
                    }
                    i++;
                }
                g.drawString(str, x + width - 2 - widthStrMode, y + 3, Graphics.RIGHT | Graphics.TOP);
            } else {
                i = textDisplay.length() - 1;
                while (true) {
                    str = textDisplay.substring(0, i);
                    if (font.stringWidth(str) <= width - 2 - widthStrMode) {
                        break;
                    }
                    i--;
                }
                g.drawString(str, x + 2, y + 3, 0);
            }
        } else {
            g.drawString(textDisplay, x + 2, y + 3, 0);
        }
        g.setColor(0x00ff00);
        g.fillRect(x + width - 1 - widthStrMode, y + 3, widthStrMode + 1, font.getHeight());
        g.setColor(colorText);
        g.drawString(strMode[mode], x + width - 2, y + 3, Graphics.RIGHT | Graphics.TOP);
    }

    public void KeyPress(int keyCode, int gameCode) {
        switch (gameCode) {
            case Canvas.LEFT:
                if (pointer > 0) {
                    pointer--;
                }
                return;
            case Canvas.RIGHT:
                if (pointer < text.length()) {
                    pointer++;
                }
                return;
        }
        switch (keyCode) {
            case -8:
                DelKey();
                break;
            case Canvas.KEY_STAR:
                mode++;
                if (mode > 2) {
                    mode = 0;
                }
                break;
            default:
                switch (mode) {
                    case 0:
                        SelectKey(keyCode, keyCodesLowCase);
                        break;
                    case 1:
                        SelectKey(keyCode, keyCodesUpCase);
                        break;
                    case 2:
                        SelectNum(keyCode);
                        break;
                }
                break;
        }
    }

    public void DelKey() {
        if (pointer == 0) {
            if (text.length() > 0) {
                text = text.substring(1);
            } else {
                text = "";
            }
            return;
        }
        String s = "";
        s += text.substring(0, pointer - 1);
        s += text.substring(pointer);
        text = s;
        pointer--;
    }

    public static String SelectKey(String value, int pointer, int keyCode) {
        String str = "";
        str += value.substring(0, pointer);
        str += (char) keyCode;
        str += value.substring(pointer);
        return str;
    }

    public void SelectNum(int keyCode) {
        String str = "";
        str += text.substring(0, pointer);
        str += keyCodesNumber[keyCode - Canvas.KEY_NUM0];
        str += text.substring(pointer);
        text = str;
        pointer++;
    }
    private long timer;
    private int timeOut = 500;
    private int lastKey;
    private int index;

    public void SelectKey(int keycode, char[][] keywords) {
        if (lastKey == keycode) {
            if (System.currentTimeMillis() - timer > timeOut) {
                timer = System.currentTimeMillis();
                index = 0;
                String str = "";
                str += text.substring(0, pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][index];
                str += text.substring(pointer);
                text = str;
                if (this.textType == TextType.PASSWORD) {
                    ResetPassword();
                    str = "";
                    str += textPass.substring(0, pointer);
                    str += keywords[keycode - Canvas.KEY_NUM0][index];
                    str += textPass.substring(pointer);
                    textPass = str;
                }
                timer = System.currentTimeMillis();
                lastKey = keycode;
                pointer++;
                return;
            } else {
                index++;
                if (index == keywords[keycode - Canvas.KEY_NUM0].length) {
                    index = 0;
                }
                if (text.length() > 0) {
                    DelKey();
                    if (this.textType == TextType.PASSWORD) {
                        String str = "";
                        for (int i = 0; i < textPass.length() - 1; i++) {
                            str += "*";
                        }
                        textPass = str;
                    }
                }
                String str = "";
                str += text.substring(0, pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][index];
                str += text.substring(pointer);
                text = str;
                if (this.textType == TextType.PASSWORD) {
                    str = "";
                    str += textPass.substring(0, pointer);
                    str += keywords[keycode - Canvas.KEY_NUM0][index];
                    str += textPass.substring(pointer);
                    textPass = str;
                }
                timer = System.currentTimeMillis();
                lastKey = keycode;
                pointer++;
                return;
            }
        } else {
            index = 0;
            String str = "";
            str += text.substring(0, pointer);
            str += keywords[keycode - Canvas.KEY_NUM0][index];
            str += text.substring(pointer);
            text = str;
            if (this.textType == TextType.PASSWORD) {
                ResetPassword();
                str = "";
                str += textPass.substring(0, pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][index];
                str += textPass.substring(pointer);
                textPass = str;
            }
            timer = System.currentTimeMillis();
            lastKey = keycode;
            pointer++;
            return;
        }
    }

    public void ResetPassword() {
        if (this.textType == TextType.PASSWORD) {
            String str = "";
            for (int i = 0; i < textPass.length(); i++) {
                str += "*";
            }
            textPass = str;
        }
    }
}
